package com.bjb.vr.operation.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bjb.vr.common.ao.OptCusDevGroupAo;
import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.dto.OptProdBundle;
import com.bjb.vr.common.dto.ResBundle;
import com.bjb.vr.common.dto.ResGroup;
import com.bjb.vr.common.result.BaseErrorCode;
import com.bjb.vr.common.result.BaseResult;
import com.bjb.vr.common.result.CommonResult;
import com.bjb.vr.common.service.OptCusDevGroupService;
import com.bjb.vr.common.service.OptProdBundleService;
import com.bjb.vr.common.service.ResBundleService;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import com.bjb.vr.common.vo.ResCusDevGroupVo;
import com.bjb.vr.common.vo.UmsAccountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * @version 1.0.0
 * @description:客户配置管理-设备组管理
 * @author: caihaibo
 * @createDate: 2022/6/17
 */
@RestController
@RequestMapping("/api/operation/v1")
public class CusDevGroupController {

    @Autowired
    OptCusDevGroupService optCusDevGroupService;

    @Resource
    OptProdBundleService optProdBundleService;


    /**
     * 客户配置管理-设备组管理-查询设备组列表
     *
     * @param optCusDevGroupAo
     * @return
     */
    @PostMapping("/queryCusConfigDevGroupList")
    public BaseResult<List<ResCusDevGroupVo>> queryCusConfigDevGroupList(@RequestBody OptCusDevGroupAo optCusDevGroupAo) {
        return CommonResult.Ok().setResult(optCusDevGroupService.queryCusConfigDevGroupList(optCusDevGroupAo));
    }


    /**
     * 客户配置管理-设备组管理-查询单个设备组
     *
     * @param optCusDevGroupAo
     * @return
     */
    @PostMapping("/queryCusConfigDevGroupOne")
    public BaseResult<ResGroup> queryCusConfigDevGroupOne(@RequestBody OptCusDevGroupAo optCusDevGroupAo) {
        Assert.notNull(optCusDevGroupAo.getId(), "分组id不能为空");

        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
        ResGroup resGroup = optCusDevGroupService.getById(optCusDevGroupAo.getId());
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, false);

        return CommonResult.Ok().setResult(resGroup);
    }

    /**
     * 客户配置管理-设备组管理-新增设备组
     *
     * @param optCusDevGroupAo
     * @return
     */
    @PostMapping("/addCusConfigDevGroup")
    public BaseResult addCusConfigDevGroup(@RequestBody OptCusDevGroupAo optCusDevGroupAo) {
        Assert.notNull(optCusDevGroupAo.getTenantId(), "租户id不能为空");
        boolean success = optCusDevGroupService.addCusConfigDevGroup(optCusDevGroupAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_ADD);
        }
        return CommonResult.Ok();
    }

    /**
     * 客户配置管理-设备组管理-编辑设备组
     *
     * @param resGroupAo
     * @return
     */
    @PostMapping("/editCusConfigDevGroup")
    public BaseResult editCusConfigDevGroup(@RequestBody OptCusDevGroupAo resGroupAo) {
        Assert.notNull(resGroupAo.getId(), "分组Id不能为空！");
        boolean success = optCusDevGroupService.editCusConfigDevGroup(resGroupAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
        }
        return CommonResult.Ok();
    }


    /**
     * 客户配置管理-设备组管理-设置设备组资源包
     *
     * @param resGroupAo
     * @return
     */
    @PostMapping("/setCusConfigDevGroupBundle")
    public BaseResult setCusConfigDevGroupBundle(@RequestBody OptCusDevGroupAo resGroupAo) {
        Assert.notNull(resGroupAo.getId(), "分组Id不能为空！");
        Assert.notNull(resGroupAo.getBundleId(), "资源包Id不能为空！");
        Assert.notNull(resGroupAo.getTenantId(), "租户id不能为空");
        UmsAccountVo umsAccountVo = (UmsAccountVo) ThreadLocalUtil.get(ThreadLocalConstant.USER_ACCOUNT_INFO);
        umsAccountVo.setTempTenantId(resGroupAo.getTenantId());
        boolean success = optCusDevGroupService.setCusConfigDevGroupBundle(resGroupAo);
        umsAccountVo.setTempTenantId("");
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
        }
        return CommonResult.Ok();
    }

    /**
     * 客户配置管理-设备组管理-移除资源包
     *
     * @param resGroupAo
     * @return
     */
    @PostMapping("/removeCusConfigDevGroupBundle")
    public BaseResult removeCusConfigDevGroupBundle(@RequestBody OptCusDevGroupAo resGroupAo) {
        Assert.notNull(resGroupAo.getId(), "分组Id不能为空！");
        Assert.notNull(resGroupAo.getTenantId(),"请选择客户！");
        Assert.notNull(resGroupAo.getBundleId(),"资源包id不能为空！");
        UmsAccountVo umsAccountVo = (UmsAccountVo) ThreadLocalUtil.get(ThreadLocalConstant.USER_ACCOUNT_INFO);
        umsAccountVo.setTempTenantId(resGroupAo.getTenantId());
        boolean success = optCusDevGroupService.removeCusConfigDevGroupBundle(resGroupAo);
        umsAccountVo.setTempTenantId("");
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
        }
        return CommonResult.Ok();
    }


    /**
     * 客户配置管理-设备组管理-获取资源包下拉列表
     * @return
     */
    @PostMapping("/queryResBundleList")
    public BaseResult queryResBundleList(@RequestBody OptCusDevGroupAo resGroupAo) {
        Assert.notNull(resGroupAo.getTenantId(), "租户id不能为空");
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
        List<OptProdBundle> resBundleList = optProdBundleService.queryResBundleList();
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, false);
        return CommonResult.Ok().setResult(resBundleList);
    }

}
